import os
import re

root_folder = r'E:\tomcat\2.dezhou\apache-tomcat_1222\apache-tomcat_1220'
save_txt = 'needchange.txt'
suffix_pro = '.properties'
suffix_xml = '.xml'
suffix_yml = '.yml'
fileList = [r'webapps\cas\WEB-INF\deployerConfigContext.xml',  # cas
                r'webapps\FQuery\WEB-INF\classes\hibernate.cfg.xml',  # foquery
                r'webapps\MsgService\WEB-INF\classes\applicationContext.xml',
                r'webapps\MsgService\WEB-INF\classes\spring-rabbitmq.xml',
                # MsgService
                r'webapps\PFFrame\flex\config\appconfig.xml',
                r'webapps\PFFrame\WEB-INF\classes\config\pfframe.properties', r'webapps\PFFrame\WEB-INF\web.xml',
                # PFFrame
                r'webapps\UniEstate\WEB-INF\classes\config\uniestate.properties',
                r'webapps\UniEstate\WEB-INF\classes\config\case-struts.xml',  # UniEstate
                r'webapps\WFService\WEB-INF\classes\DBcfg.xml']  # wfservice
re_ip = re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
re_estate =re.compile( r'(estate(_[a-z]{2})?)')
re_pfuser = re.compile(r'(pfuser(_[a-z]{2})?)')

replace_ip='127.0.0.1'
replace_pfuser='pfuser_ml'
relplace_estate='estate_ml'


def record(folder, save_txt=None):
    save_file = open(save_txt, 'a', encoding='utf-8')
    for name in os.listdir(folder):
        if os.path.isdir(os.path.join(folder, name)):
            record(os.path.join(folder, name), save_txt)
        elif name.endswith(suffix_pro) or name.endswith(suffix_xml) or name.endswith(suffix_yml):
            save_file.write('{}\n'.format(os.path.join(folder, name)))

    save_file.close()


def initFloders():
    fileList = [r'webapps\cas\WEB-INF\deployerConfigContext.xml',  # cas
                r'webapps\FQuery\WEB-INF\classes\hibernate.cfg.xml',  # foquery
                r'webapps\MsgService\WEB-INF\classes\applicationContext.xml',
                r'webapps\MsgService\WEB-INF\classes\spring-rabbitmq.xml',
                # MsgService
                r'webapps\PFFrame\flex\config\appconfig.xml',
                r'webapps\PFFrame\WEB-INF\classes\config\pfframe.properties', r'webapps\PFFrame\WEB-INF\web.xml',
                # PFFrame
                r'webapps\UniEstate\WEB-INF\classes\config\uniestate.properties',
                r'webapps\UniEstate\WEB-INF\classes\config\case-struts.xml',  # UniEstate
                r'webapps\WFService\WEB-INF\classes\DBcfg.xml']  # wfservice
    re_ip = r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
    re_estate = r'(estate(_[a-z]{2})?)'
    re_pfuser = r'(pfuser(_[a-z]{2})?)'


def beginReplace():
    print('beginRelpace')
    print(fileList)
    for name in fileList:
        print(name)
        fullpath = os.path.join(os.path.abspath(root_folder), name)
        try:
            content=''
            with open(fullpath, 'r') as f:
                content = f.read()
                content = re_ip.sub(replace_ip, content)
                content = re_estate.sub(relplace_estate, content)
                content = re_pfuser.sub(replace_pfuser, content)
                print('替换够的',content)

            with open(fullpath,'w') as wf:
                print('需要加入的内容',content)
                wf.write(content)


        except Exception as e:
            print('打开文件{}出错'.format(fullpath), e)


def main():


    # record(os.path.abspath(root_folder),save_txt)
    initFloders()
    beginReplace()


if __name__ == '__main__':
    main()
